package com.etc.controller;


import com.etc.entity.Movies;
import com.etc.entity.Orders;
import com.etc.service.MoviesService;
import com.etc.service.OrdersServiceImpl;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrdersController {
    Logger logger = Logger.getLogger(OrdersController.class);
    @Autowired
    OrdersServiceImpl ordersServiceImpl;

    @Autowired
    MoviesService moviesService;

    /**
     * 查询所有
     *
     * @param user_no  用户编号
     * @param pageNum  页数
     * @param pageSize 数量
     * @return pageinfo
     */
    @RequestMapping("/queryAllByUser")
    @ResponseBody
    public PageInfo<Orders> queryAllByUser(Integer user_no, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        PageInfo<Orders> pageInfo = ordersServiceImpl.queryAllByUser(user_no, pageNum, pageSize);
        return pageInfo;
    }

    /**
     * 查询所有
     *
     * @param showing_no 展厅编号
     * @param pageNum    页数
     * @param pageSize   数量
     * @return pageinfo
     */
    @RequestMapping("/queryAllByMovie")
    @ResponseBody
    public PageInfo<Orders> queryAllByMovie(Integer showing_no, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        PageInfo<Orders> pageInfo = ordersServiceImpl.queryAllByShowing(showing_no, pageNum, pageSize);
        return pageInfo;
    }
/*
    @RequestMapping("/queryDetail")
    @ResponseBody
    public Orders queryDetail(Integer user_no, Integer showing_no) {
        Orders orders = ordersServiceImpl.queryDetail(user_no, showing_no);
        return orders;
    }*/

    /**
     * 添加订单
     *
     * @param orders order信息
     * @return boolean
     */
    @RequestMapping("/addOrder")
    @ResponseBody
    public boolean addOrder(Orders orders) {
        boolean b = ordersServiceImpl.addOrder(orders);
        return b;
    }

    /**
     * 删除订单
     *
     * @param order_no 订单编号
     * @return boolean
     */
    @RequestMapping("/delOrder")
    @ResponseBody
    public boolean query(Integer order_no) {
        boolean b = ordersServiceImpl.delOrder(order_no);
        return b;
    }

    /**
     * 查询
     *
     * @param user_no  用户编号
     * @param pageNum  页数
     * @param pageSize 数量
     * @return pageinfo
     */
    @RequestMapping("/queryByUser")
    @ResponseBody
    public PageInfo<Orders> queryByUser(Integer user_no, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        PageInfo<Orders> pageInfo = ordersServiceImpl.queryByUser(user_no, pageNum, pageSize);
        return pageInfo;
    }

    /**
     * 查询
     *
     * @param movie_no 电影编号
     * @param pageNum  页数
     * @param pageSize 数量
     * @return pageinfo
     */
    @RequestMapping("/queryByMovie")
    @ResponseBody
    public PageInfo<Orders> queryByMovie(Integer movie_no, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        PageInfo<Orders> pageInfo = ordersServiceImpl.queryByMovie(movie_no, pageNum, pageSize);
        return pageInfo;
    }

    /**
     * 详细
     *
     * @param order_no 订单编号
     * @return order
     */
    @RequestMapping("/queryDetail")
    @ResponseBody
    public Orders queryDetail(String order_no) {
        Orders orders = ordersServiceImpl.queryDetail(order_no);
        return orders;
    }

    /**
     * 查询
     *
     * @param order_no 订单编号
     * @return order
     */
    @RequestMapping("/queryByOrder")
    @ResponseBody
    public Orders queryByOrder(String order_no) {
        Orders orders = ordersServiceImpl.queryByOrder(order_no);
        return orders;
    }

    /**
     * 跳转到订单列表
     *
     * @return String
     */
    @RequestMapping("/toOrderList")
    public String toOrderList() {
        return "front/orderList";
    }

    /**
     * 详情
     *
     * @param order_no 订单编号
     * @return modelandview
     */
    @RequestMapping("/toOrderDetail")
    public ModelAndView toOrderDetail(String order_no) {
        logger.debug("看看进来了没有");
        ModelAndView modelAndView = new ModelAndView();
        Orders orders = ordersServiceImpl.queryDetail(order_no);
        modelAndView.addObject("orders", orders);
        logger.debug("看看进来了没有orders:" + orders);
        modelAndView.addObject("mov", orders.getShowings().getMovies());
        modelAndView.setViewName("front/orderDtail");
        return modelAndView;
    }

    @RequestMapping("/addOrders")
    public void addOrders(Orders orders) {


    }

    /**
     * 查询数量
     *
     * @return list
     */
    @RequestMapping("/queryAllCount")
    @ResponseBody
    public List<Movies> queryAllCount() {
        List<Movies> list = ordersServiceImpl.queryAllCount();
        return list;
    }
}
